package bip.QFKRapprove;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.alibaba.fastjson.JSONObject;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.comn.NetStreamContext;
import nc.bs.framework.core.service.IFwLogin;
import nc.itf.trn.regmng.IRegmngQueryService;
import nc.itf.trn.transmng.ITransmngQueryService;
import nc.itf.uap.pf.IWorkflowMachine;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.login.bs.INCUserQueryService;
import nc.vo.pub.workflownote.WorkflownoteVO;
import nc.vo.sm.UserVO;
import nc.vo.trn.regmng.AggRegapplyVO;
import nc.vo.trn.transmng.AggStapply;
import nc.vo.uap.pf.PfProcessBatchRetObject;
import nccloud.pubitf.riart.pflow.CloudPFlowContext;
import nccloud.pubitf.riart.pflow.ICloudScriptPFlowService;
import nccloud.ws.rest.resource.AbstractNCCRestResource;

@Path("/hrtrn/baseinfo/Approvalresults")
public class ApproveResources extends AbstractNCCRestResource {
	BaseDAO dao=new BaseDAO();
	@Override
	public String getModule() {
		// TODO Auto-generated method stub
		return "hrtrn";
	}
	//调动
	@POST
	@Path("/Transfer")
	@Consumes({ "application/json" })
	@Produces({ "application/json" })
	public JSONObject Transfer(JSONObject object)  throws Exception {
		JSONObject result = new JSONObject();
		try {
			init();
			String pk_hi_regapplysql="select pk_hi_stapply from hi_stapply where bill_code = '"+object.getString("applycode")+"'";
			String pk_hi_regapply=  (String)dao.executeQuery(pk_hi_regapplysql, new ColumnProcessor());
			AggStapply aggvo = NCLocator.getInstance().lookup(ITransmngQueryService.class).queryByPk(pk_hi_regapply);
			if(aggvo==null) {
				result.put("success", "false");
				result.put("message", "未找到对应申请单");
				return result;
			}
			String ApproveStatus = object.getString("approvestatus");

			CloudPFlowContext paramCloudPFlowContext = new CloudPFlowContext();
			paramCloudPFlowContext.setBillVos(new AggStapply[] {aggvo});
			paramCloudPFlowContext.setBillType("6113");

			if(ApproveStatus==null) {
				result.put("success", "false");
				result.put("message", "审批状态不可为空");
				return result;
			}else if(ApproveStatus.equals("Y")){
				IWorkflowMachine iWorkfowMachine = NCLocator.getInstance().lookup(IWorkflowMachine.class);
				WorkflownoteVO wfVO = iWorkfowMachine.checkWorkFlow("APPROVE" + "1001E210000000007XRT", "6113", aggvo, null);
				wfVO.setChecknote("批准");
				wfVO.setApproveresult("Y");
				new BaseDAO().updateVO(wfVO);

				paramCloudPFlowContext.setActionName("APPROVE");
				Object[] results = NCLocator.getInstance().lookup(ICloudScriptPFlowService.class).exeScriptPFlow(paramCloudPFlowContext);
				if(results[0] instanceof AggStapply) {
					result.put("success", "true");
					result.put("message", "申请单编码："+((AggStapply)results[0]).getParentVO().getAttributeValue("bill_code").toString()+",审批完成");}
				else if(results[0] instanceof PfProcessBatchRetObject) {
					result.put("success", "false");
					result.put("message", ((PfProcessBatchRetObject)results[0]).getExceptionMsg());
				}
			}else if(ApproveStatus.equals("N")){
				IWorkflowMachine iWorkfowMachine = NCLocator.getInstance().lookup(IWorkflowMachine.class);
				WorkflownoteVO wfVO = iWorkfowMachine.checkWorkFlow("APPROVE" + "1001E210000000007XRT", "6113", aggvo, null);
				wfVO.setChecknote("不通过");
				wfVO.setApproveresult("N");
				new BaseDAO().updateVO(wfVO);

				paramCloudPFlowContext.setActionName("APPROVE");
				Object[] results = NCLocator.getInstance().lookup(ICloudScriptPFlowService.class).exeScriptPFlow(paramCloudPFlowContext);
				if(results[0] instanceof AggStapply) {
					result.put("success", "true");
					result.put("message", "申请单编码："+((AggStapply)results[0]).getParentVO().getAttributeValue("bill_code").toString()+",审批完成");}
				else if(results[0] instanceof PfProcessBatchRetObject) {
					result.put("success", "false");
					result.put("message", ((PfProcessBatchRetObject)results[0]).getExceptionMsg());
				}
			}}catch (Exception e){
				result.put("success", "false");
				result.put("message", e.getMessage()==null?"审批流异常":e.getMessage());
			}

		return result;
	}
	//转正
	@POST
	@Path("/regularization")
	@Consumes({ "application/json" })
	@Produces({ "application/json" })
	public JSONObject regularization(JSONObject object)  throws Exception {
		JSONObject result = new JSONObject();
		try {
			init();
			String pk_hi_regapplysql="select pk_hi_regapply from hi_regapply where bill_code = '"+object.getString("applycode")+"'";
			String pk_hi_regapply=  (String)dao.executeQuery(pk_hi_regapplysql, new ColumnProcessor());
			AggRegapplyVO aggvo = NCLocator.getInstance().lookup(IRegmngQueryService.class).queryByPk(pk_hi_regapply);
			if(aggvo==null) {
				result.put("success", "false");
				result.put("message", "未找到对应申请单");
				return result;
			}
			String ApproveStatus = object.getString("approvestatus");

			CloudPFlowContext paramCloudPFlowContext = new CloudPFlowContext();
			paramCloudPFlowContext.setBillVos(new AggRegapplyVO[] {aggvo});
			paramCloudPFlowContext.setBillType("6111");

			if(ApproveStatus==null) {
				result.put("success", "false");
				result.put("message", "审批状态不可为空");
				return result;
			}else if(ApproveStatus.equals("Y")){
				IWorkflowMachine iWorkfowMachine = NCLocator.getInstance().lookup(IWorkflowMachine.class);
				WorkflownoteVO wfVO = iWorkfowMachine.checkWorkFlow("APPROVE" + "1001E210000000007XRT", "6111", aggvo, null);
				wfVO.setChecknote("批准");
				wfVO.setApproveresult("Y");
				new BaseDAO().updateVO(wfVO);

				paramCloudPFlowContext.setActionName("APPROVE");
				Object[] results = NCLocator.getInstance().lookup(ICloudScriptPFlowService.class).exeScriptPFlow(paramCloudPFlowContext);
				if(results[0] instanceof AggRegapplyVO) {
					result.put("success", "true");
					result.put("message", "申请单编码："+((AggRegapplyVO)results[0]).getParentVO().getAttributeValue("bill_code").toString()+",审批完成");}
				else if(results[0] instanceof PfProcessBatchRetObject) {
					result.put("success", "false");
					result.put("message", ((PfProcessBatchRetObject)results[0]).getExceptionMsg());
				}
			}else if(ApproveStatus.equals("N")){
				IWorkflowMachine iWorkfowMachine = NCLocator.getInstance().lookup(IWorkflowMachine.class);
				WorkflownoteVO wfVO = iWorkfowMachine.checkWorkFlow("APPROVE" + "1001E210000000007XRT", "6111", aggvo, null);
				wfVO.setChecknote("不通过");
				wfVO.setApproveresult("N");
				new BaseDAO().updateVO(wfVO);

				paramCloudPFlowContext.setActionName("APPROVE");
				Object[] results = NCLocator.getInstance().lookup(ICloudScriptPFlowService.class).exeScriptPFlow(paramCloudPFlowContext);
				if(results[0] instanceof AggRegapplyVO) {
					result.put("success", "true");
					result.put("message", "申请单编码："+((AggRegapplyVO)results[0]).getParentVO().getAttributeValue("bill_code").toString()+",审批完成");}
				else if(results[0] instanceof PfProcessBatchRetObject) {
					result.put("success", "false");
					result.put("message", ((PfProcessBatchRetObject)results[0]).getExceptionMsg());
				}
			}}catch (Exception e){
				result.put("success", "false");
				result.put("message", e.getMessage()==null?"审批流异常":e.getMessage());
			}

		return result;
	}
	//离职
	@POST
	@Path("/resignation")
	@Consumes({ "application/json" })
	@Produces({ "application/json" })
	public JSONObject resignation(JSONObject object)  throws Exception {
		JSONObject result = new JSONObject();
		try {
			init();
			String pk_hi_regapplysql="select pk_hi_stapply from hi_stapply where bill_code = '"+object.getString("applycode")+"'";
			String pk_hi_regapply=  (String)dao.executeQuery(pk_hi_regapplysql, new ColumnProcessor());
			AggStapply aggvo = NCLocator.getInstance().lookup(ITransmngQueryService.class).queryByPk(pk_hi_regapply);
			if(aggvo==null) {
				result.put("success", "false");
				result.put("message", "未找到对应申请单");
				return result;
			}
			String ApproveStatus = object.getString("approvestatus");

			CloudPFlowContext paramCloudPFlowContext = new CloudPFlowContext();
			paramCloudPFlowContext.setBillVos(new AggStapply[] {aggvo});
			paramCloudPFlowContext.setBillType("6115");

			if(ApproveStatus==null) {
				result.put("success", "false");
				result.put("message", "审批状态不可为空");
				return result;
			}else if(ApproveStatus.equals("Y")){
				IWorkflowMachine iWorkfowMachine = NCLocator.getInstance().lookup(IWorkflowMachine.class);
				WorkflownoteVO wfVO = iWorkfowMachine.checkWorkFlow("SIGNAL" + "1001E210000000007XRT", "6115", aggvo, null);
				wfVO.setChecknote("批准");
				wfVO.setApproveresult("Y");
				new BaseDAO().updateVO(wfVO);

				paramCloudPFlowContext.setActionName("SIGNAL");
				Object[] results = NCLocator.getInstance().lookup(ICloudScriptPFlowService.class).exeScriptPFlow(paramCloudPFlowContext);
				if(results[0] instanceof AggStapply) {
					result.put("success", "true");
					result.put("message", "申请单编码："+((AggStapply)results[0]).getParentVO().getAttributeValue("bill_code").toString()+",审批完成");}
				else if(results[0] instanceof PfProcessBatchRetObject) {
					result.put("success", "false");
					result.put("message", ((PfProcessBatchRetObject)results[0]).getExceptionMsg());
				}
			}else if(ApproveStatus.equals("N")){
				IWorkflowMachine iWorkfowMachine = NCLocator.getInstance().lookup(IWorkflowMachine.class);
				WorkflownoteVO wfVO = iWorkfowMachine.checkWorkFlow("SIGNAL" + "1001E210000000007XRT", "6115", aggvo, null);
				wfVO.setChecknote("不通过");
				wfVO.setApproveresult("N");
				new BaseDAO().updateVO(wfVO);

				paramCloudPFlowContext.setActionName("SIGNAL");
				Object[] results = NCLocator.getInstance().lookup(ICloudScriptPFlowService.class).exeScriptPFlow(paramCloudPFlowContext);
				if(results[0] instanceof AggStapply) {
					result.put("success", "true");
					result.put("message", "申请单编码："+((AggStapply)results[0]).getParentVO().getAttributeValue("bill_code").toString()+",审批完成");}
				else if(results[0] instanceof PfProcessBatchRetObject) {
					result.put("success", "false");
					result.put("message", ((PfProcessBatchRetObject)results[0]).getExceptionMsg());
				}
			}}catch (Exception e){
				result.put("success", "false");
				result.put("message", e.getMessage()==null?"审批流异常":e.getMessage());
			}

		return result;
	}




	private void init() throws Exception {
		// 模拟登陆
		String usercode = "SP";// 账号
		String psw = "1qaz2wsx!";// 密码
		String ds = "design";// 数据源

		INCUserQueryService service = NCLocator.getInstance().lookup(INCUserQueryService.class);
		UserVO user = service.findUserVO(ds, usercode);

		InvocationInfoProxy.getInstance().setUserDataSource(ds);
		InvocationInfoProxy.getInstance().setGroupId(user.getPk_group());
		InvocationInfoProxy.getInstance().setUserCode(user.getUser_code());
		InvocationInfoProxy.getInstance().setUserId(user.getCuserid());
		IFwLogin ls = NCLocator.getInstance().lookup(IFwLogin.class);

		byte[] token = ls.login(usercode, psw, null);
		NetStreamContext.setToken(token);
	}

}
